<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use Myredis\Myredis;
use app\admin\controller\Admincontroller;

class Goods extends Admincontroller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if(!empty(input('search'))){
           $search=input('search');
           $list=Db::table('xo_goods_up')->field(true)->where('pname','like','%'.$search."%")->where(['check'=>'1'])->paginate(10);
           $page=$list->render();
           return view('index',['list'=>$list,'page'=>$page]);
        }

        if(!empty(input('type'))){
            $list=Db::table('xo_goods_up')->field(true)->where('row','>','0')->order('row','asc')->paginate(10);
            $page=$list->render();
            return view('index',['list'=>$list,'page'=>$page]);
        }

        $p=input('page');
        $row=10;
        $aa=Db::table('xo_goods_up')->field(true)->order('addtime','desc')->where(['check'=>'1'])->paginate($row);
        $page=$aa->render();


        $list=Myredis::get('checkpage');
        if(empty($list)){
            for ($i=1;$i<=5;$i++){
                $list[$i]=Db::table('xo_goods_up')->field(true)->order('addtime','desc')->limit(($i-1)*$row,$row)->where(['check'=>'1'])->select();
            }
            Myredis::setex('checkpage',10800,$list);
        }

        switch ($p)
        {
            case 0:
                $list=$list[1];
                break;

            case 1:
                $list=$list[1];
  break;
            case 2:
                $list=$list[2];
  break;
            case 3:
                $list=$list[3];
  break;
            case 4:
                $list=$list[4];
  break;
            case 5:
                $list=$list[5];
  break;
            default:
                $list=Db::table('xo_goods_up')->field(true)->order('addtime','desc')->limit(($p-1)*$row,$row)->select();
                ;
        }
        return view('index',['list'=>$list,'page'=>$page]);
    }

    public function uncheck()
    {
        $list=Db::table('xo_goods_up')->field(true)->order('addtime','asc')->where(['check'=>'2'])->select();
        return view('goods/uncheck',['list'=>$list]);
    }

    public function check()
    {
        if(!Request::instance()->isPost())
        {
            return $this->error('非法请求',url('admin/goods/index'));
        }

        $id=input('post.id');
        $status=input('post.status');
        $data['check']=$status;
        $data['row']='0';

        $resulte=DB::table('xo_goods_up')->where(['id'=>$id])->update($data);
        if($resulte>0){
            $list['status']=true;
            $list['info']=$status;
        }else{
            $list['status']=false;
        }
        return json($list);

    }

    public function banner()
    {
        $list=Db::table('xo_admin_banner')->field(true)->order('addtime','desc')->select();
        return view('goods/banner',['list'=>$list]);
    }

    public function dela()
    {
        if (!Request::instance()->isPost()){
            return $this->error('非法请求');
        }


        $data=input('post.');
        if(empty($data['info'])){
            return $this->error('您还没删除任何东西哦');
        }
        $info=$data['info'];

        $n=count($info);
        $num=0;
        foreach($info as $key =>$val)
        {
            $num+=Db::table('xo_goods_up')->where(['id'=>$val])->delete();
            Db::table('xo_user_goods')->where(['gid'=>$val])->delete();
            Db::table('xo_user_down')->where(['gid'=>$val])->delete();
        }



        if($num==$n){
            return $this->success('批量删除成功');
        }else{
            return $this->error('批量删除失败');
        }





    }

        public function changeurl()
        {
            if(!Request::instance()->isAjax()){
                return $this->error('非法请求',url('admin/index/index'));
            }

            $data=input('post.');
            $id=$data['id'];
            $url['url']=$data['url'];

            $res=Db::table('xo_admin_banner')->where(['id'=>$id])->update($url);
            if($res>0){
                $result['status']=true;
                $result['info']='修改成功';
            }else{
                $result['status']=false;
                $result['info']='修改失败,请稍后再试';
            }
            return json($result);
        }









    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if(!$request->isPost()){
            return redirect('index/error/index');
        }

        $name=input('session.admin.name');

        $this->upload($name);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        if(!Request::instance()->isAjax())
        {
            return $this->error('非法请求',url('admin/goods/index'));
        }

        $id=input('delete.id');

        $res=Db::table('xo_goods_up')->delete($id);

        if($res>0){
            $data['status']=true;
        }else{
            $data['status']=false;
        }

        return json($data);


    }




    public function push()
    {
        if(!Request::instance()->isPost())
        {
            return $this->error('非法请求',url('admin/goods/index'));
        }

        $id=input('post.id');
        $status=input('post.status');
        $data['status']=$status;

        $resulte=DB::table('xo_admin_banner')->where(['id'=>$id])->update($data);
        if($resulte>0){
            $list['status']=true;
            $list['info']=$status;
        }else{
            $list['status']=false;
        }
        return json($list);

    }


    public function delbanner()
    {
        if(!Request::instance()->isAjax())
        {
            return $this->error('非法请求',url('admin/goods/index'));
        }

        $id=input('delete.id');

        $res=Db::table('xo_admin_banner')->delete($id);

        if($res>0){
            $data['status']=true;
        }else{
            $data['status']=false;
        }

        return json($data);


    }

    public function row()
    {
        if(!Request::instance()->isAjax()){
            return $this->error('非法请求',url('admin/index/index'));
        }


        $id=input('post.id');
        $row=input('post.row');

        if($row==0){
            $result['row']=$row;
            $list=Db::table('xo_goods_up')->where(['id'=>$id])->update($result);
            if($list>0){
                $data['status']=true;
                $data['info']='已取消推送';
                $data['num']='取消';
            }else{
                $data['status']=false;
                $data['info']='该图片已是未推送状态,取消失败';
            }
            return json($data);

        }

        $res=Db::table('xo_goods_up')->field('row')->where(['id'=>$id])->find();

        $oldrow=$res['row'];

        $num=Db::table('xo_goods_up')->where(['row'=>$row])->count('id');
        if($num==4){
            $data['status']=false;
            $data['info']='此推送行已有4个图片,请整理后再添加';
            return json($data);
        }
        if($oldrow==$row){
         $data['status']=false;
         $data['info']='该图片已经是首页第'.$row.'行的内容,推送失败';
         return json($data);
        }

        $result['row']=$row;
        $list=Db::table('xo_goods_up')->where(['id'=>$id])->update($result);
        if($list>0){
            $data['status']=true;
            $data['info']='已推送至首页第'.$row.'行';
        }else{
            $data['status']=false;
        }
        return json($data);
    }
}
